package com.example.doer.reading.AppActivities;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.view.sliding.AbSlidingPlayView;
import com.example.doer.reading.AppAdapters.StoreBookListAdapter;
import com.example.doer.reading.AppDataModel.Book;
import com.example.doer.reading.R;
import com.example.doer.reading.loading.lib.AdhesionLoadingView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import static com.ab.util.AbToastUtil.showToast;


public class BookStoreFragment extends Fragment {
    AbSlidingPlayView abSlidingPlayView=null;
    private GridView gvRecommend;
    private GridView gvNew;
    private GridView gvHot;
    private View view;
    private List<Book> reCommendBooks;
    private StoreBookListAdapter recommendAdapter;

    private List<Book> hotBooks;
    private StoreBookListAdapter hotAdapter;

    private List<Book> newBooks;
    private StoreBookListAdapter newAdapter;

    private AdhesionLoadingView adhesionLoadingView;
    AbHttpUtil abHttpUtil;


    public BookStoreFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.view=inflater.inflate(R.layout.fragment_book_store, container, false);
        adhesionLoadingView= (AdhesionLoadingView) view.findViewById(R.id.adhesionLoadingView);
        addSlidingView(inflater,container);

//        abSlidingPlayView.setOnItemClickListener(new AbSlidingPlayView.AbOnItemClickListener() {
//            @Override
//            public void onClick(int i) {
//                String url=null;
//                if(i ==0)
//                {
//                    url="http://192.168.191.1/Reading/BookStoreServlet?Action=Single_Book&BookID=0001";
//                }
//                if(i == 1)
//                {
//                    url="http://192.168.191.1/Reading/BookStoreServlet?Action=Single_Book&BookID=0003";
//                }
//                if(i==2)
//                {
//                    url="http://192.168.191.1/Reading/BookStoreServlet?Action=Single_Book&BookID=0007";
//                }
//                abHttpUtil.get(url, new AbStringHttpResponseListener() {
//                    @Override
//                    public void onSuccess(int i, String s) {
//                        try {
//                            JSONObject object=new JSONObject(s);
//                            Book book=new Book(object.getString("BookID"),
//                                    object.getString("BookName"),
//                                    object.getString("Category"),
//                                    object.getString("BookDate"),
//                                    object.getString("BookAuthor"),
//                                    object.getString("BookDescription"),
//                                    object.getInt("BookPrice"));
//                            Intent intent = new Intent(getActivity(),BookDetailActivity.class);
//                            intent.putExtra("book",book);
//                            startActivity(intent);
//
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//
//                    }
//
//                    @Override
//                    public void onStart() {
//
//                    }
//
//
//                    @Override
//                    public void onFinish() {
//                        adhesionLoadingView.setVisibility(View.INVISIBLE);
//                    }
//
//                    @Override
//                    public void onFailure(int i, String s, Throwable throwable) {
//                        adhesionLoadingView.setVisibility(View.INVISIBLE);
//                        showToast(getActivity(),"加载失败");
//                    }
//
//                });
//            }
//        });
        setLayout();
        abHttpUtil=AbHttpUtil.getInstance(getActivity());
        String url="http://192.168.191.1/Reading/BookStoreServlet?Action=InitStorePage";
        abHttpUtil.get(url, new AbStringHttpResponseListener() {
            @Override
            public void onSuccess(int i, String s) {
                try {

                    JSONObject jsonObject=new JSONObject(s);
                    JSONArray recArray=jsonObject.getJSONArray("Recommend");
                    JSONArray hotArray=jsonObject.getJSONArray("Hot");
                    JSONArray newArray=jsonObject.getJSONArray("New");
                    Gson gson=new Gson();
                    reCommendBooks = gson.fromJson(recArray.toString(),new TypeToken<List<Book>>(){}.getType());
                    recommendAdapter=new StoreBookListAdapter(getActivity(),reCommendBooks);
                    gvRecommend.setAdapter(recommendAdapter);
                    hotBooks = gson.fromJson(hotArray.toString(),new TypeToken<List<Book>>(){}.getType());
                    hotAdapter=new StoreBookListAdapter(getActivity(),hotBooks);
                    gvHot.setAdapter(hotAdapter);
                    newBooks = gson.fromJson(newArray.toString(),new TypeToken<List<Book>>(){}.getType());
                    newAdapter=new StoreBookListAdapter(getActivity(),newBooks);
                    gvNew.setAdapter(newAdapter);
                    showToast(getActivity(),"加载成功");

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {
                adhesionLoadingView.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {
                adhesionLoadingView.setVisibility(View.INVISIBLE);
                showToast(getActivity(),"加载失败");
            }
        });








        return view;
    }

    public void addSlidingView(LayoutInflater inflater,ViewGroup container){
        abSlidingPlayView = (AbSlidingPlayView)view.findViewById(R.id.mAbSlidingPlayView);


        final View mPlayView = inflater.inflate(R.layout.play_view_item, container,false);
        ImageView mPlayImage = (ImageView) mPlayView.findViewById(R.id.play_View);
        TextView mPlayText = (TextView) mPlayView.findViewById(R.id.play_text);

        mPlayImage.setBackgroundResource(R.drawable.zetianji);

        final View mPlayView1 = inflater.inflate(R.layout.play_view_item, container,false);
        ImageView mPlayImage1 = (ImageView) mPlayView1.findViewById(R.id.play_View);
        TextView mPlayText1 = (TextView) mPlayView1.findViewById(R.id.play_text);

        mPlayImage1.setBackgroundResource(R.drawable.wukongzhuan);

        final View mPlayView2 = inflater.inflate(R.layout.play_view_item, container,false);
        ImageView mPlayImage2 = (ImageView) mPlayView2.findViewById(R.id.play_View);
        TextView mPlayText2 = (TextView) mPlayView2.findViewById(R.id.play_text);

        mPlayImage2.setBackgroundResource(R.drawable.baiduren);
        abSlidingPlayView.setNavPageResources(R.drawable.play_display,R.drawable.play_hide);
        abSlidingPlayView.setNavHorizontalGravity(Gravity.RIGHT);
        abSlidingPlayView.addView(mPlayView);
        abSlidingPlayView.addView(mPlayView1);
        abSlidingPlayView.addView(mPlayView2);

        abSlidingPlayView.setOnItemClickListener(new AbSlidingPlayView.AbOnItemClickListener() {

            @Override
            public void onClick(int position) {
                String url=null;
                if(position ==0)
                {
                    url="http://192.168.191.1/Reading/BookStoreServlet?Action=Single_Book&BookID=0001";
                }
                if(position == 1)
                {
                    url="http://192.168.191.1/Reading/BookStoreServlet?Action=Single_Book&BookID=0003";
                }
                if(position==2)
                {
                    url="http://192.168.191.1/Reading/BookStoreServlet?Action=Single_Book&BookID=0007";
                }
                abHttpUtil.get(url, new AbStringHttpResponseListener() {
                    @Override
                    public void onSuccess(int i, String s) {
                        try {
                            JSONObject jsonobject=new JSONObject(s);
                            JSONObject object=jsonobject.getJSONObject("book");
                            Book book=new Book(object.getString("bookID"),
                                    object.getString("bookName"),
                                    object.getString("category"),
                                    object.getString("bookDate"),
                                    object.getString("bookAuthor"),
                                    object.getString("bookDescription"),
                                    object.getInt("bookPrice"));
                            Intent intent = new Intent(getContext(),BookDetailActivity.class);
                            intent.putExtra("book",book);
                            startActivity(intent);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onStart() {

                    }


                    @Override
                    public void onFinish() {
                        adhesionLoadingView.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onFailure(int i, String s, Throwable throwable) {
                        adhesionLoadingView.setVisibility(View.INVISIBLE);
                        showToast(getActivity(),"加载失败");
                    }

                });
            }
        });

        abSlidingPlayView.setOnPageChangeListener(new AbSlidingPlayView.AbOnChangeListener() {

            @Override
            public void onChange(int position) {
                //showToast(getActivity(),"改变"+position);
            }
        });

        abSlidingPlayView.startPlay();
    }

    public void setLayout(){
        RelativeLayout layout1=(RelativeLayout)this.view.findViewById(R.id.Store_Layout1);
        ImageView imageView1=(ImageView)layout1.findViewById(R.id.imageView);
        imageView1.setImageResource(R.drawable.category);
        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),CategorysActivity.class);
                startActivity(intent);
            }
        });
        RelativeLayout layout2=(RelativeLayout)this.view.findViewById(R.id.Store_Layout2);
        ImageView imageView2=(ImageView)layout2.findViewById(R.id.imageView2);
        imageView2.setImageResource(R.drawable.changxiao);
        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),OtherBooks.class);
                intent.putExtra("ifIsCategoryList",false);
                intent.putExtra("Category","HotSaleList");
                startActivity(intent);
            }
        });
        RelativeLayout layout3=(RelativeLayout)this.view.findViewById(R.id.Store_Layout3);
        ImageView imageView3=(ImageView)layout3.findViewById(R.id.imageView3);
        imageView3.setImageResource(R.drawable.xinshu);
        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),OtherBooks.class);
                intent.putExtra("ifIsCategoryList",false);
                intent.putExtra("Category","NewBookList");
                startActivity(intent);
            }
        });
        RelativeLayout layout4=(RelativeLayout)this.view.findViewById(R.id.Store_Layout4);
        ImageView imageView4=(ImageView)layout4.findViewById(R.id.imageView4);
        imageView4.setImageResource(R.drawable.tuijian);
        layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),OtherBooks.class);
                intent.putExtra("ifIsCategoryList",false);
                intent.putExtra("Category","GoodCommentList");
                startActivity(intent);
            }
        });
        gvRecommend = (GridView) view.findViewById(R.id.gvRecommend);
        gvHot = (GridView) view.findViewById(R.id.gvHot);
        gvNew = (GridView) view.findViewById(R.id.gvNew);
        setListeners();



    }
    private void setListeners() {
        gvRecommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Book book = reCommendBooks.get(position);
                Intent intent=new Intent(getActivity(), BookDetailActivity.class);
                intent.putExtra("book",book);
                startActivity(intent);
            }
        });

        gvHot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Book book = hotBooks.get(position);
                Intent intent=new Intent(getActivity(), BookDetailActivity.class);
                intent.putExtra("book",book);
                startActivity(intent);
            }
        });

        gvNew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Book book = newBooks.get(position);
                Intent intent=new Intent(getActivity(), BookDetailActivity.class);
                intent.putExtra("book",book);
                startActivity(intent);
            }
        });


    }
}
